package com.zjmzxfzhl.modules.app.controller;

import com.alibaba.fastjson.JSON;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.modules.app.service.AppModiService;
import com.zjmzxfzhl.modules.app.service.ListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/listener")
public class ListenerController {

    @Autowired
    private ListenerService listenerService;

    @PostMapping("/updateLeaveStatus")
    public void updateLeaveStatus(@RequestBody String json) {
        Map jsonMap = JSON.parseObject(json);
        String businessKey = jsonMap.get("businessKey").toString();
        if(businessKey.indexOf(":")!=-1){
        String type=businessKey.substring(0,businessKey.indexOf(":"));
        String typeId= businessKey.substring(businessKey.indexOf(":")+1);
            switch (type) {
                case "modi": //变更结束
                    listenerService.modiLeaveStatus(typeId);
                    break;
                case "doc": //文档结束
                    listenerService.docLeaveStatus(typeId);
                    break;
                case "prodVer": //产品发布结束
                    listenerService.prodVerLeaveStatus(typeId);
                    break;
                default:
            }
        }
    }

    /**
     * 审核流程
     * @param json
     */
    @PostMapping("/processListener")
    public void processListener(@RequestBody String json) {
        Map jsonMap = JSON.parseObject(json);
        String businessKey = jsonMap.get("businessKey").toString();
        String user = jsonMap.get("user").toString();
        if(businessKey.indexOf(":")!=-1){
            String type=businessKey.substring(0,businessKey.indexOf(":"));
            String typeId= businessKey.substring(businessKey.indexOf(":")+1);
            switch (type) {
                case "modi": //变更同意
                    listenerService.modiProcessRecord(typeId,user);
                    break;
                case "doc": //文档同意
                    listenerService.docProcessRecord(typeId,user);
                    break;
                case "prodVer": //产品发布同意
                    listenerService.prodVerProcessRecord(typeId,user);
                    break;
                default:
            }
        }
    }

    /**
     * 驳回监听
     * @param json
     */
    @PostMapping("/rejectListener")
    public void rejectListener(@RequestBody String json) {
        Map jsonMap = JSON.parseObject(json);
        String businessKey = jsonMap.get("businessKey").toString();
        String user = jsonMap.get("user").toString();
        if(businessKey.indexOf(":")!=-1){
            String type=businessKey.substring(0,businessKey.indexOf(":"));
            String typeId= businessKey.substring(businessKey.indexOf(":")+1);
            switch (type) {
                case "modi": //变更驳回
                    listenerService.modiRejectRecord(typeId,user);
                    break;
                case "doc": //文档驳回
                    listenerService.docRejectRecord(typeId,user);
                    break;
                case "prodVer": //产品发布驳回
                    listenerService.prodVerRejectRecord(typeId,user);
                    break;
                default:
            }
        }
    }

    /**
     * 停止监听
     * @param json
     */
    @PostMapping("/stopListener")
    public void stopListener(@RequestBody String json) {
        Map jsonMap = JSON.parseObject(json);
        String businessKey = jsonMap.get("businessKey").toString();
        if(businessKey.indexOf(":")!=-1){
            String type=businessKey.substring(0,businessKey.indexOf(":"));
            String typeId= businessKey.substring(businessKey.indexOf(":")+1);
            switch (type) {
                case "modi": //变更停止
                    listenerService.modiStopStatus(typeId);
                    break;
                case "doc": //文档停止
                    listenerService.docStopStatus(typeId);
                    break;
                case "prodVer": //产品发布停止
                    listenerService.prodVerStopStatus(typeId);
                    break;
                default:
            }
        }
    }
}
